﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccessLayer;
using BussinessLayer;

namespace QLBaoHanh.TraHang
{
    public partial class frmThemLuuY : Form
    {
        private bool _isEditForm;
        private int _phien_ThietBiBHId;
        private int _lichSuId;
        private frmDsTraThietBiBHSC _callerForm;

        public frmThemLuuY(bool inIsEditForm, int inPhien_ThietBiBHId, frmDsTraThietBiBHSC inCallerForm, int inLichSuId = 0)
        {
            InitializeComponent();
            _isEditForm = inIsEditForm;
            _phien_ThietBiBHId = inPhien_ThietBiBHId;
            _lichSuId = inLichSuId;
            _callerForm = inCallerForm;
            txtTenNhanVien.Text = frmMain.CurrentNhanVien.Ma + " - " + frmMain.CurrentNhanVien.Ten;
        }

        private void FillForm()
        {
            if(_isEditForm)
            {
                this.Text = "Sửa lưu ý";
                //get and fill data to form
                LichSuBH obj = LichSuBHBO.getInstance.GetLichSuBHById(_lichSuId);
                txtNoiDung.Text = obj.NoiDung;
            }
            else
            {
                //
                this.Text = "Thêm lưu ý";
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            LichSuBH obj;
            if (_isEditForm)
            {
                //Update lich su bao hanh
                obj = LichSuBHBO.getInstance.GetLichSuBHById(_lichSuId);
                obj.NVThucHienId = frmMain.CurrentNhanVien.Id;
                obj.NoiDung = txtNoiDung.Text;
                LichSuBHBO.getInstance.UpdateLichSuBH(obj);
            } 
            else
            {
                //Them moi lich su
                obj = new LichSuBH();
                obj.LoaiLichSu = Convert.ToInt16(MyCommon.LoaiLichSu.LichSuComment);
                obj.Phien_ThietBiBHId = _phien_ThietBiBHId;
                obj.ThoiGian = DateTime.Now;
                obj.NVThucHienId = frmMain.CurrentNhanVien.Id;
                obj.NoiDung = txtNoiDung.Text;
                LichSuBHBO.getInstance.InsertLichSuBH(obj);
            }
            _callerForm.LoadGridGhiChu(_phien_ThietBiBHId);
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmThemLuuY_Load(object sender, EventArgs e)
        {
            FillForm();
        }
    }
}
